package com.qzf.test.model;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

public class EleBox {
    private static List<Brand> slot =new ArrayList<>();
    private static Map gameMap; // 引用游戏地图
    
    // 设置游戏地图引用
    public static void setGameMap(Map map) {
        gameMap = map;
    }

    public static void deleteByBrandName(String name){
        Iterator<Brand> iterator =slot.iterator();
        while (iterator.hasNext()){
            Brand b = iterator.next();
            if(b.getName().equals(name)){
                b.getParent().remove(b);
                iterator.remove();
            }
        }
    }

    public static void add(Brand brand){
        slot.add(brand);
        slot.sort(Comparator.comparing(Brand::getName));
        java.util.Map<String, List<Brand>> listMap = slot.stream().collect(Collectors.groupingBy(Brand::getName));
        Set<String> set =listMap.keySet();
        for(String s:set){
            List<Brand> brandList=listMap.get(s);
            if (brandList.size()==3){
                deleteByBrandName(s);
                break;
            }
        }
        paint();
        over(brand);
        
        // 更新地图中卡牌的可见性状态
        if (gameMap != null) {
            gameMap.compareALL();
        }
    }
    public static void paint(){
        for(int i=0;i<slot.size();i++){
            Brand brand=slot.get(i);
            int x = i*brand.getWidth()+10;
            brand.setBounds(x,500,50,50);
        }
    }
    public static void over(Brand b){
        if(slot.size()>=7){
            JOptionPane.showMessageDialog(b,"Game Over");
            System.exit(0);
        }
    }
}
